// pages/school/compare/index.js
const {
  schoolList,
  schoolAdd,
  majorPageList
} = require('../../../api/api')

const {
  checkLogin,
  filterObj,
  returnLabel
} = require('../../../utils/util')
const selectorRouteDatas = [{
    id: 0,
    title: "全国",
    type: "radiobox",
    key: "province",
    items: [{
        id: 0,
        title: "全国"
      },
      {
        id: 1,
        title: "福建"
      },
      {
        id: 2,
        title: "北京"
      },
      {
        id: 3,
        title: "上海"
      },
      {
        id: 4,
        title: "四川"
      },
      {
        id: 5,
        title: "湖南"
      },
    ]
  },
  {
    id: 1,
    title: "专业",
    type: "checkbox",
    key: "major",
    items: []
  },
  {
    id: 3,
    title: "排序",
    key: "orderBy",
    type: 'radiobox',
    items: [{
        id: 0,
        title: "默认排序",
        checked: true
      }, {
        id: 'scoreDesc',
        title: "分数由高到低",
      },
      {
        id: 'scoreAsc',
        title: "分数由低到高",
      },
      {
        id: 'hotDesc',
        title: "热度由高到低",
      },
      {
        id: 'workRateDesc',
        title: "就业率由高到低",
      },
      {
        id: 'upRateDesc',
        title: "国内升学率由高到底",
      },
    ],
  },
  {
    id: 4,
    title: "其他",
    key: "batch",
    type: 'other',
    items: [],
  },


]
const filterBatch = [{
    id: 3,
    title: "办学层次",
    key: "eduLevelName",
    type: 'radiobox',
    items: [{
        id: '本科',
        title: "本科",
        checked: true
      }, {
        id: '专科（高职）',
        title: "专科（高职）",
      },

    ],
  },
  {
    id: 3,
    title: "办学类型",
    key: "eduType",
    type: 'checkbox',
    items: [{
        id: '公办学校',
        title: "公办学校",
        checked: true
      }, {
        id: '民办学校',
        title: "民办学校",
      },
      {
        id: '中外合作办学',
        title: "中外合作办学",
      },
      {
        id: '内地港澳台合作办学',
        title: "内地港澳台合作办学",
      },
    ],
  },
  {
    id: 3,
    title: "院校特色",
    key: "schoolLabel",
    type: 'checkbox',
    items: [{
        id: 985,
        title: "985",
      }, {
        id: 211,
        title: "211",
      },
      {
        id: '双一流',
        title: "双一流",
      },
      {
        id: '强基计划',
        title: "强基计划",
      },
    ],
  },
  {
    id: 4,
    title: "院校类型",
    key: "schoolType",
    type: 'checkbox',
    items: [{
        id: '综合类',
        title: "综合类",
      }, {
        id: '理工类',
        title: "理工类",
      },
      {
        id: '农林类',
        title: "农林类",
      },
      {
        id: '医药类',
        title: "医药类",
      },
      {
        id: '师范类',
        title: "师范类",
      },
    ],
  },
]


function filterArr(selected, selectData) {
  let c = selectData.filter((item) => {
    return (selected.indexOf(item.id) > -1)
  })
  return c
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    list: [],
    currentRegion: {
      name: '全国',
      code: 0
    },
    pageTNo: 1,
    pageTSize: 20,
    filterBatch: filterBatch,
    selectedData: {
      province: 0,
      batch: 1,
      major: [],
      orderBy: 0,
      eduLevelName: 0,
      eduType: [],
      schoolLabel: [],
      schoolType: [],
      selectedMajorList: []
    },
    selectorDataList: selectorRouteDatas,
    showSelect: false,
    type: {
      name: '本科',
      id: 2,
    },
    pageNo: 1,
    loginVisible: true,
    pageSize: 20,
    filterRouteKey: "compareFilters",
    selectedMajorList: [],
    bottomLift: getApp().globalData.bottomLift,
    compareList: [],
    key: 'add',
    schoolName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      filterRouteKey,
      selectedData
    } = this.data
    wx.setStorageSync(filterRouteKey, selectedData)
    this.fetchCity()
    this.getMajorPageList()
  },
  inputChange(e) {
    this.setData({
      schoolName: e.detail.value
    })
  },
  async fetchCity() {
    const {
      filterBatch,
      selectorDataList
    } = this.data
    const eduTypeList = wx.getStorageSync('edu_type') || await getApp().getDictData('edu_type')
    const schoolLabelList = wx.getStorageSync('school_label') || await getApp().getDictData('school_label')
    const schoolTypeList = wx.getStorageSync('school_type') || await getApp().getDictData('school_type')
    const cityList = wx.getStorageSync('cityList') || await getApp().getCityList()
    cityList.unshift({
      id: 0,
      title: '全国',
      checked: true
    })
    selectorDataList[0].items = cityList
    filterBatch[1].items = eduTypeList
    filterBatch[2].items = schoolLabelList
    filterBatch[3].items = schoolTypeList
    this.setData({
      filterBatch,
      selectorDataList
    })
    this.filterKeyFunc()
  },
  scroll() {
    const {
      selectorDataList,
      pageTSize
    } = this.data
    const count = selectorDataList[1].items.length - 1
    if (selectorDataList[1].items[count].length < pageTSize) {
      return
    }

    this.getMajorPageList(true)
  },
  searchMaj(e) {
    const {
      name
    } = e.detail
    const {
      selectorDataList
    } = this.data
    selectorDataList[1].items = []
    this.setData({
      name,
      pageTNo: 1,
      selectorDataList
    })
    this.getMajorPageList(true)
  },
  async getMajorPageList(flag) {
    const {
      pageTNo,
      pageTSize,
      name,
      selectorDataList,
    } = this.data
    let params = {
      name,
      pageNo: pageTNo,
      pageSize: pageTSize,
    }
    params = filterObj(params)
    const {
      result
    } = await majorPageList(params)
    result.records = result.records.map(e => {
      return {
        id: e.specialId,
        title: e.name,
        desc: e.level1Name + '-' + e.level2Name + '-' + e.level3Name,
      }
    })
    selectorDataList[1].items = selectorDataList[1].items.concat(result.records)
    if (result.records.length == 0) {
      return
    }
    this.setData({
      selectorDataList,
      pageTNo: pageTNo + 1,
    })
    if (flag !== undefined) {
      this.selectComponent("#selector").syncData1(selectorDataList);
    }

  },
  //修改筛选条件--执行onloadFunc
  filterKeyFunc() {
    const {
      selectorDataList,
      filterRouteKey
    } = this.data
    const selectedData = wx.getStorageSync(filterRouteKey)
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = (item.key == 'province') ? c.length > 0 ? c[0].title : item.title : item.title
    })
    this.setData({
      selectorDataList,
      selectedData
    })
    this.filterMajor()

  },
  //修改专业--执行onloadFunc
  filterMajor() {
    const {
      selectorDataList,
      filterRouteKey
    } = this.data
    const selectedData = wx.getStorageSync(filterRouteKey)

    const selectorMajorList = selectorDataList[1].items
    const selectedMajorList = filterArr(selectedData['major'], selectorMajorList)
    this.setData({
      selectedMajorList
    })
    // checkLogin().then((res) => {
      this.onLoadFunc()
    // })
  },
  clear() {
    this.selectComponent("#selector").clear();
    //   const {
    //     filterRouteKey
    //   } = this.data
    //   const selectedData = wx.getStorageSync(filterRouteKey)
    //   selectedData.major = []
    //   selectedData.selectedMajorList = []
    //   wx.setStorageSync(filterRouteKey, selectedData)
    //   this.setData({
    //     selectedData,
    //     selectedMajorList: []
    //   })
    //   this.filterMajor()
  },
  clearItem(e) {
    this.selectComponent("#selector").clearItem(e);
    //   const {
    //     filterRouteKey,
    //     selectedMajorList
    //   } = this.data

    //   let {
    //     id
    //   } = e.currentTarget.dataset
    //   var selectedData = wx.getStorageSync(filterRouteKey)
    //   const newList = selectedMajorList.filter((item) => {
    //     return item.id !== id
    //   })
    //   let major = selectedData.major
    //   major.splice(major.indexOf(id), 1)
    //   selectedData.selectedMajorList=newList
    //   wx.setStorageSync(filterRouteKey, selectedData)
    //   this.setData({
    //     selectedData,
    //     selectedMajorList: newList
    //   })
    //   this.filterMajor()
  },
  deleteItem(e) {
    const {
      compareList
    } = this.data
    const {
      id,
      list
    } = e.detail
    compareList.map((item, index) => {
      if (item.id == id) {
        compareList.splice(index, 1)
      }
    })
    this.setData({
      compareList,
      list
    })
  },
  addItem(e) {
    const {
      compareList
    } = this.data
    compareList.push(e.detail.value)
    this.setData({
      compareList,
      list: e.detail.list
    })
  },
  compare() {
    const {
      compareList
    } = this.data
    if (compareList.length < 2) {
      wx.showToast({
        title: '至少选择两个学校',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/school/compare_checked/index?params=' + encodeURIComponent(JSON.stringify(compareList)),
    })
  },
  async onSelectChangedRoute(e) {
    const {
      filterRouteKey
    } = this.data
    wx.setStorageSync(filterRouteKey, {
      ...e.detail
    });
    this.filterKeyFunc()
  },
  onLoadFunc() {
    this.setData({
      pageNo: 1,
      list: []
    })
    this.getList()
  },
  async getList() {
    this.setData({
      loader: true
    })
    const {
      type,
      pageNo,
      pageSize,
      filterRouteKey,
      schoolName
    } = this.data
    let {
      province,
      major,
      orderBy,
      eduLevelName,
      schoolLabel,
      eduType,
      schoolType
    } = wx.getStorageSync(filterRouteKey)
    eduType = eduType.join(',')
    schoolType = schoolType.join(',')
    let params = {
      pageNo,
      pageSize,
      provincesCode: province,
      specialtyIds: major,
      orderBy,
      eduLevelName,
      schoolType,
      name: schoolName,
      eduType
    }
    let obj = wx.getStorageSync('school_label_obj')
    let newObj = {}
    schoolLabel.map(e => {
      if (obj[e]) {
        newObj[obj[e]] = 1
      }
    })
    params = Object.assign(newObj, params)
    params = filterObj(params)
    // console.log(params)
    const {
      result
    } = await schoolList(params)
    if (result.records.length == 0) {
      this.setData({
        loader: false
      })
      return
    }
    result.records.map((item) => {
      item['filter'] = returnLabel(item)
    })

    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
      loader: false
    })
  },
  changeIndex(e) {

  },
  onReachBottom() {
    const {
      list,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) {
      return
    }
    this.getList()
  },
  checkAll() {
    var {
      list,
      isAllChecked,
      compareList
    } = this.data
    const newArray = []
    list.map((item) => {
      item.map((child) => {
        child.isCompare = !isAllChecked
        newArray.push(child)
      })
    })
    //清空
    compareList = isAllChecked ? [] : newArray
    this.setData({
      isAllChecked: !this.data.isAllChecked,
      list,
      compareList
    })
  },
  goto(e) {
    const {
      url,
    } = e.currentTarget.dataset
    wx.navigateTo({
      url
    })
  },
  chooseCity() {
    wx.navigateTo({
      url: '/pages/list/index?type=school&category=city',
    })
  },
  getImageLogo() {
    return 'https://static-data.gaokao.cn/upload/logo/42.jpg'
  },
  chooseType() {
    this.setData({
      showSelect: true
    })
  },
  onShareAppMessage() {
    return {
      title: '院校对比',
      imageUrl: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})
